<!DOCTYPE html>
<html lang="en" style="">
<head>
	<title>08-Config | PhalApi - A light-weight PHP framework for API</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="icon" href="http://webtools.qiniudn.com/dog_catch.png" type="image/x-icon" />
	<meta name="description" content="PhalApiis A light-weight PHP framework for API!">
	<meta name="keywords" content="PhalApi,phalapi,phalapi API develop,backend framework,restful,web services,saas,microservice,API framework,open API framework,PHP API framework,php web framework">
	<meta name="author" content="dogstar">

	<link rel="stylesheet" type="text/css" href="./../..//css/screen.css?20150211" />
</head>

<body>
<a href="https://github.com/phalapi/phalapi">
	<img alt="Fork me on GitHub" data-canonical-src="http://7xslqv.com1.z0.glb.clouddn.com/images/2/2d/9e69c0ebaff22a24e241a0244532e.png" src="http://7xslqv.com1.z0.glb.clouddn.com/images/2/2d/9e69c0ebaff22a24e241a0244532e.png" style="position: absolute; top: 0; right: 0; border: 0;">
</a>

<!-- 最顶部的语言(S) -->
<div class="grid-wrapper navbar desktop-only">
	<div class="grid">
		<div class="grid__cell">
			<ul id="language-switchers" class="navbar__links navbar--left">
                <li class="menu-item"><a title="PhalApi官方网站中文版" href="/" class="menu-item__link">Chinese</a></li>
				<li class="menu-item"><a title="English version of PhalApi website" href="/wikis/en/" class="menu-item__link">English</a></li>
			</ul>
			<ul id="util-menu" class="navbar__links navbar--right navbar--vertical-separator">
				<li class="menu-item"><a href="#" class="menu-item__link contact-us-spec">Welcome Here!</a></li>
			</ul>
		</div>
	</div>
</div>
<!-- 最顶部的语言(E) -->

<!-- 顶部导航菜单(S) -->
<div class="grid-wrapper desktop-only">
	<div class="grid">
		<div class="grid__cell">
			<div class="header__header-wrapper">
				<a title="PhalApi" href="http://www.phalapi.net" class="header__logo"><img src="http://webtools.qiniudn.com/master-LOGO-20150410_33.jpg" id="tw-logo" alt="PhalApi"></a>
				<ul id="main-menu" class="header__menu">
					<li class="menu-item"><a href="http://qa.phalapi.net/" class="menu-item__link insights-spec" target="_blank">Community</a></li>
					<li class="menu-item"><a href="https://github.com/phalapi/phalapi" class="menu-item__link insights-spec" target="_blank">Download</a></li>
					<li class="menu-item"><a href="/wikis/en/" class="menu-item__link events-spec" target="_blank">Wikis</a></li>
    				<li class="menu-item"><a href="/docs_en/" class="menu-item__link events-spec" target="_blank">Classes docs</a></li>
					<li class="menu-item"><a href="http://demo.phalapi.net/" class="menu-item__link products-spec" target="_blank">Demo</a></li>
					<li class="menu-item"><a href="/about.html" class="menu-item__link about-us-spec" target="_blank">About us</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
<!-- 顶部导航菜单(E) -->


    <div class="grid-wrapper">
        <div class="grid">
            <div class="grid__cell">
                <h2>8.1 Simple Config Retrieving</h2>
<p>Generally, there are three config files in PhalApi.  </p>
<pre><code>$ tree ./PhalAPi/Config
.
├── app.php
├── dbs.php
└── sys.php</code></pre>
<p>The file <code>app.php</code> contains project configs, <code>dbs.php</code> includes database configs, and <code>sys.php</code> kepp system configs for different environments.  </p>
<p>We can specify where are these config files in entrance file.  </p>
<pre><code>//$ vim ./Public/init.php

// Configurations
DI()-&gt;config = new PhalApi_Config_File(API_ROOT . '/Config');</code></pre>
<p>Assume there are some configs as below in file <code>app.php</code>.  </p>
<pre><code>return array(

    'version' =&gt; '1.1.1',

    'email' =&gt; array(
        'address' =&gt; 'chanzonghuang@gmail.com',
    );
);</code></pre>
<p>Then we can retrieve configs simply.  </p>
<pre><code>// all configs in app.php
DI()-&gt;config-&gt;get('app');  // array( ... )

// one config in app.php
DI()-&gt;config-&gt;get('app.version');  // 1.1.1

// multi level config in app.php
DI()-&gt;config-&gt;get('app.email.address');  // 'chanzonghuang@gmail.com'</code></pre>
<p>We can also add new config files as we want. </p>
<h2>8.2 Config Management Under Continuous Intergration</h2>
<p>Actually, there are serveral enviroments during project development, such as development enviroment, testing enviroment, production enviroment etc. Consequently, we need different config files for different enviroment.  </p>
<p>Usually, we maintain different config files with different suffixes. Take production configs as an example. The <code>Config</code> folder looks like as below after we add new prod files <code>dbs.php.prod</code> and <code>sys.php.prod</code>:   </p>
<pre><code>$ tree ./PhalApi/Config
.
├── app.php
├── dbs.php
├── dbs.php.prod
├── sys.php
└── sys.php.prod</code></pre>
<p>After that, we can switch to related config files automatically in deployment. Take <a href="https://github.com/phingofficial/phing">PHing</a> for example.  </p>
<p>We could replace these config files with production config files in <code>build.xml</code>.  </p>
<pre><code>//$ vim ./build.xml 

        &lt;copy 
            file="./Config/dbs.php.prod" 
            tofile="./Config/dbs.php" 
            overwrite="true" /&gt;
        &lt;copy 
            file="./Config/sys.php.prod" 
            tofile="./Config/sys.php" 
            overwrite="true" /&gt;</code></pre>
<p>Once we excute <code>phing</code> to deploy, we will see:  </p>
<pre><code>     [copy] Copying 1 file to /home/phapapi/Config
     [copy] Copying 1 file to /home/phapapi/Config</code></pre>
<h2>8.3 Using Yaconf Under PHP7</h2>
<h3>(1) PHP7 Installation</h3>
<p>If you have PHP7 installed, you can ignore this part, or you need to install PHP7 first of all.  </p>
<h3>(2) Installing Yaconf library</h3>
<p>Please visit <a href="https://github.com/laruence/yaconf">laruence/yaconf</a>.  </p>
<h3>(3) Register <code>DI()-&gt;config</code> with <code>PhalApi_Config_Yaconf</code></h3>
<p>At entrance file, let's register <code>DI()-&gt;config</code> with <a href="https://github.com/phalapi/phalapi/blob/release-en/PhalApi/PhalApi/Config/Yaconf.php">PhalApi_Config_Yaconf</a>.  </p>
<pre><code>DI()-&gt;config = new PhalApi_Config_Yaconf();</code></pre>
<h3>(4) Usage</h3>
<p>Assume we have a <code>test.ini</code> as below in <code>./Config</code> folder.  </p>
<pre><code>//$ vim ./Config/test.ini

name="PhalApi"
version="1.3.1"</code></pre>
<p>Then we can retrieve the configs as usual.  </p>
<pre><code>var_dump(DI()-&gt;config-&gt;get('foo')); // same as var_dump(Yaconf::get("foo"));

var_dump(DI()-&gt;config-&gt;has('foo')); // same as var_dump(Yaconf::has("foo"));</code></pre>
<h2>8.4 Extend Your Own Config</h2>
<p>PhalApi save config into files at default. Of course, we can store our configs at anywhere, like database, cache, even remote servers wherever we want. All we need to do is just implementing the interface <a href="https://github.com/phalapi/phalapi/blob/release-en/PhalApi/PhalApi/Config.php">PhalApi_Config</a>.</p>
            </div>
        </div>
    </div>
    
 <!-- 广告位 -->
<!--
<div class="grid-wrapper desktop-only">
	<p align="center">
		<a href="http://7qnay5.com1.z0.glb.clouddn.com/走向开源的第一年%20-%20PhalApi%202015年度开源总结%20-%20官方出品%20-%2020151214.pdf" target="blank"><img width="950" height="100" src="http://7qnay5.com1.z0.glb.clouddn.com/PhalApi20151213-2.jpg"></a>
		<a href="http://qa.phalapi.net" target="blank"><img width="950" height="100" src="http://7qnay5.com1.z0.glb.clouddn.com/qa_ad_20150615.jpg"></a>
	</p>
</div>
-->

<!-- footer(S) -->
<div class="grid-wrapper footer">
	<div id="footer" class="grid">	

		<div class="grid__cell unit-1-2--lap">
			<h3><img src="http://webtools.qiniudn.com/master-LOGO-20150410_50.jpg" height="50"></h3>
            <p>
            <font size="3px;">PhalApi is a light-weight PHP framework for API.<br/>
                We try to keep PhalApi as stuip simple as possible!
            </font>
			</p>
			<p align="left">
				<font size="2px">&copy;2015-2016 PhalApi All Rights Reserved. 粤ICP备15028808号</font>
				<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1255326144'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1255326144%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script>
			</p>
		</div>	

		<div class="grid__cell unit-1-2--lap">
			<h3>Thanks!</h3>

			<div class="nav">
				<ul class="footer__nav">
					<li class="menu-item"><a href="http://www.oschina.net/" class="menu-item__link" target="_blank">Open Source China</a></li>
					<li class="menu-item"><a href="http://www.phalconphp.com/en/" class="menu-item__link" target="_blank">Phalcon</a></li>
					<li class="menu-item"><a href="https://phpunit.de/manual/3.7/zh_cn/automating-tests.html" class="menu-item__link" target="_blank">PHPUnit</a></li>
					<li class="menu-item"><a href="http://www.thoughtworks.com/cn/" class="menu-item__link" target="_blank">ThoughtWorks</a></li>
				</ul>
			</div>

            <div id="perspectives">
                <div class="email-signup">
                    <strong><a href="https://auth.alipay.com/login/index.htm" target="_blank">Alipay&nbsp;</a>Donate: </strong>chanzonghuang@gmail.com
                </div>
            </div>

			<p>
				<!-- JiaThis Button BEGIN -->
				<div id="ckepop">
				<span class="jiathis_txt">Share: </span>
				<a class="jiathis_button_tsina">Weibo</a>
				<a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">More</a>
				<a class="jiathis_counter_style"></a>
				</div>
				<script type="text/javascript" src="http://v2.jiathis.com/code/jia.js" charset="utf-8"></script>
				<!-- JiaThis Button END -->
				
			</p>
		</div>
	</div>
</div>
<!-- footer(E) -->

</body>

<script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1254743218'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "w.cnzz.com/q_stat.php%3Fid%3D1254743218' type='text/javascript'%3E%3C/script%3E"));</script>

</html>
